package com.yxh.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yxh.R;
import com.yxh.adapter.ClassSelectAdapter;
import com.yxh.dto.LessonDto;
import com.yxh.service.Task;
import com.yxh.view.CommonDialog;

public class ClassSelectActivity extends BaseActivity implements
		OnClickListener {

	private ListView listView;
	private ClassSelectAdapter adapter;
	private List<LessonDto> list;
	private String lessonIds;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_class_select);

		initData();

		initView();

		overridePendingTransition(R.anim.enter_righttoleft, R.anim.noanim);
	}

	@SuppressWarnings("unchecked")
	private void initData() {
		list = (ArrayList<LessonDto>) getIntent().getSerializableExtra("list");
		if (list == null) {
			list = new ArrayList<LessonDto>();
		}
//		LogUtil.e("list length-->" + list.size());
	}

	public void initView() {

		/*** top view ***/
		((TextView) findViewById(R.id.head_layout_tv)).setText("选择课程");

		findViewById(R.id.btn).setOnClickListener(this);

		listView = (ListView) findViewById(R.id.listView);
		adapter = new ClassSelectAdapter(mContext, list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int pos,
					long arg3) {
				if (list.get(pos).isSelected.equals("0")) {
					list.get(pos).isSelected = "1";
					((ImageView) view.findViewById(R.id.img)).setEnabled(true);
				} else {
					list.get(pos).isSelected = "0";
					listView.getChildAt(pos).findViewById(R.id.img)
							.setEnabled(false);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn:
			goToSelect();
			break;
		default:
			break;
		}
	}

	private void goToSelect() {
		lessonIds = "";
		for (int i = 0; i < list.size(); i++) {
			if ("1".equals(list.get(i).isSelected)) {
				if (!TextUtils.isEmpty(lessonIds))
					lessonIds += ",";
				lessonIds += list.get(i).lessonId;
			}
		}

//		LogUtil.e("lessonIds -->" + lessonIds);

		if (TextUtils.isEmpty(lessonIds)) {
			showToast("请选择课程");
			return;
		}

		showAddClassDialog();

	}

	private void showAddClassDialog() {
		CommonDialog dialog = new CommonDialog(this,CommonDialog.NORAML);
		dialog.setTitle("声明");
		dialog.setContent(getString(R.string.add_class_text));
		dialog.setOkContent("确定添加");
		dialog.setCancleContent("取消");
		// dialog.setCancelable(false);
		dialog.setOkOnClick(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CommonDialog dialog = (CommonDialog) v.getTag();
				dialog.dismiss();

				LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
				map.put("s", "lesson/add_lesson");
				map.put("lesson_ids", lessonIds);
				getData(map, Task.ADD_LESSON, 1);
			}
		});
		dialog.setCancleOnClick(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CommonDialog dialog = (CommonDialog) v.getTag();
				dialog.dismiss();

			}

		});
		dialog.show();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh(Object... param) {
		try {
			int operType = (Integer) param[0];
			Object obj = param[1];
			closeDialog();
			switch (operType) {
			case Task.ADD_LESSON:
				if (obj != null) {
					List<LessonDto> list = (List<LessonDto>) obj;
					if ("1".equals(list.get(0).result)) {
						String[] addLessonArr = lessonIds.split(",");
						if (addLessonArr.length == 1) {
							/** 设置界面 **/
							Intent it = new Intent(mContext,
									ClassTimeSettingActivity.class);
							it.putExtra("resource", "1");
							for (int i = 0; i < list.size(); i++) {
								if (lessonIds.equals(list.get(i).lessonId)) {
									it.putExtra("dto",
											(Serializable) list.get(i));
								}
							}
							startActivity(it);
							finish();
						} else if (addLessonArr.length >= 2) {
							/** 我的课程 **/
							Intent it = new Intent(mContext,
									AboutMeClassActivity.class);
							it.putExtra("resource", "1");
							it.putExtra("list", (Serializable) list);
							startActivity(it);
							finish();
						}

					} else
						showToast(list.get(0).message);

				} else {
					showToast("网络连接异常,请检查网络");
				}
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}